<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="生产日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
              style="width: 240px"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="销售单号" prop="moeSodh">
            <el-input
              v-model="queryParams.moeSodh"
              placeholder="请输入销售单号"
              clearable
              style="width: 240px"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造单号" prop="moeZzdh">
            <el-input
              v-model="queryParams.moeZzdh"
              placeholder="请输入制造单号"
              clearable
              style="width: 240px"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="moeJtbh">
            <el-input
              v-model="queryParams.moeJtbh"
              placeholder="请输入机台编号"
              clearable
              style="width: 240px"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="itmPmgg">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.itmPmgg"
              clearable
              size="small"
              style="width: 500px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel('#exportGdMx', '工单调整记录')"
          size="small"
          >明细数据</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel('#exportGdTotal', '工单调整统计信息')"
          size="small"
        >
          统计信息
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          id="exportGdMx"
          :height="tableH"
          @scrollBottom="load"
          :data="tzlLists"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="机台"
            align="center"
            width="135"
            prop="moe_jtbh"
          />
          <el-table-column
            label="制造单号"
            align="center"
            width="140"
            prop="moe_zzdh"
          />
          <el-table-column
            label="销售单号"
            align="center"
            width="140"
            prop="moe_sodh"
          />
          <el-table-column
            label="物料代码"
            align="center"
            prop="moe_wldm"
            width="135"
          />
          <el-table-column
            label="品名规格"
            align="center"
            prop="itm_pmgg"
            width="280"
          />
          <el-table-column label="模具编号" align="center" prop="moe_mjbh" />
          <el-table-column label="计划数量" align="center" prop="moe_scsl" />
          <el-table-column label="调整数量" align="center" prop="tzl_tzsl" />
          <el-table-column label="调整类别" align="center" prop="tzl_tzlb" />
          <el-table-column
            label="类别名称"
            :show-overflow-tooltip="true"
            align="center"
            prop="tzl_tzlb"
          >
            <template #default="scope">
              <dict-tag :options="adjustOptions" :value="scope.row.tzl_tzlb" />
            </template>
          </el-table-column>
          <el-table-column label="调整原因" align="center" prop="tzl_tzyy" />
          <el-table-column label="调整人员" align="center" prop="tzl_jlrymc" />
          <el-table-column
            label="调整时间"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="tzl_jlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.tzl_jlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="统计信息" name="second">
        <el-table
          border
          stripe
          :height="tableH"
          id="exportGdTotal"
          @scrollBottom="load"
          :data="tzlListTotal"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="调整类别"
            align="center"
            width="135"
            prop="tzl_tzlb"
          />
          <el-table-column
            label="类别名称"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="tzl_tzlb"
          >
            <template #default="scope">
              <dict-tag :options="adjustOptions" :value="scope.row.tzl_tzlb" />
            </template>
          </el-table-column>
          <el-table-column label="调整数量" prop="tzl_tzsl" width="280" />
          <el-table-column
            label="调整次数"
            :show-overflow-tooltip="true"
            align="center"
            prop="tzl_cnt"
            width="180"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { adjustRecordList } from "@/api/production/adjustReport";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "AdjustRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      tableH: document.body.clientHeight - 300,
      activeTab: "first",
      tzlLists: [],
      tzlListTotal: [],
      //调整类别字典
      adjustOptions: [],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      // 弹出层标题
      title: "",
      // 建立日期时间范围
      dateRange: [
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 60),
        new Date(new Date().getTime() + 3600 * 1000 * 24 * 3650),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        moeZzdh: null,
        moeSodh: null,
        moeWldm: null,
        itmPmgg: null,
        index: 0,
      },
      // 表单校验
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //调整类别
    this.getAllDicts("production_numAdjust").then((response) => {
      this.adjustOptions = response.data;
    });
  },
  methods: {
    /** 查询工单调整记录查询 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginDate"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDate"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      adjustRecordList(this.queryParams).then((response) => {
        //明细
        this.tzlLists = [];
        this.tzlListTotal = [];
        this.tzlLists = this.tzlLists.concat(response.data.list1.rows);
        this.tzlListTotal = this.tzlListTotal.concat(response.data.list2.rows);
        this.total = response.data.list1.total;
        this.total2 = response.data.list2.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        adjustRecordList(this.queryParams).then((response) => {
          //明细
          this.tzlLists = this.tzlLists.concat(response.data.list1.rows);
          this.tzlListTotal = this.tzlListTotal.concat(
            response.data.list2.rows
          );
          this.total = response.data.list1.total;
          this.total2 = response.data.list2.total;
          this.loading = false;
        });
      }
      return 0;
    },

    handleSearch() {
      this.openSearch = true;
      this.title = "查询工单调整记录";
    },
    /** 取消按钮 */
    cancel() {
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 导出Excel */
    handleExcel(a, b) {
      exportTable(a, b);
    },
  },
};
</script>
